import moment from 'moment';

export const shortTime = function(value) {
  return moment(value).format('YYYY-MM-DD');
}

export const time = function(value) {
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
}

export const leaveTime = function(value) {
  return moment(value).format('YYYY-MM-DD HH:mm');
}

export const monthTime = function(value) {
  return moment(value).format('YYYY-MM');
}

export const secondsTime = function(value) {
  return moment(value).format('HH:mm:ss');
}

export const filterTime = (time, type = 'short') => {
  if (type === 'short') {
    return moment(time).format('YYYY-MM-DD')
  } else {
    return moment(time).format('YYYY-MM-DD HH:mm:ss')
  }
}

export const startOfDate = function(d, dateType = 'day') {
  return moment(d).startOf(dateType)
}

export const endOfDate = function(d, dateType = 'day') {
  return moment(d).endOf(dateType)
}

export const lastDateofMonth = function(d) {
  var firstDate = moment(d).startOf('month').format('YYYY-MM-DD');
  var endDate = moment(d).endOf('month').format('YYYY-MM-DD');
  var Datearr = [];
  Datearr.push(firstDate);
  Datearr.push(endDate);
  return Datearr;
}

// 获取给定日期的 n个月后的日期  给定日期格式如：val = 2019-02-26; n = 多少个月
export const computeYmd = function(val, n) {
  const str = val.split('-');
  const d = new Date(str[0], str[1], str[2]);
  // 因为getMonth()获取的月份的值只能在0~11之间所以我们在进行setMonth()之前先给其减一
  d.setMonth((d.getMonth() - 1) + n);
  const yy1 = d.getFullYear();
  let mm1 = d.getMonth() + 1;
  let dd1 = d.getDate() - 1;
  if (dd1 === '00') {
    mm1 = parseInt(mm1) - 1;
    const new_date = new Date(yy1, mm1, 1);
    dd1 = (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate()
  }
  if (mm1 < 10) {
    mm1 = '0' + mm1;
  }
  if (dd1 < 10) {
    dd1 = '0' + dd1;
  }
  return yy1 + '-' + mm1 + '-' + dd1;
}
